package com.hiyiin.IntelligentHospital.hosp.controller.api;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hiyiin.IntelligentHospital.common.exception.IntelligentHospitalException;
import com.hiyiin.IntelligentHospital.common.helper.HttpRequestHelper;
import com.hiyiin.IntelligentHospital.common.result.Result;
import com.hiyiin.IntelligentHospital.common.result.ResultCodeEnum;
import com.hiyiin.IntelligentHospital.common.util.MD5;
import com.hiyiin.IntelligentHospital.hosp.service.DepartmentService;
import com.hiyiin.IntelligentHospital.hosp.service.HospitalService;
import com.hiyiin.IntelligentHospital.hosp.service.HospitalSetService;
import com.hiyiin.IntelligentHospital.hosp.service.ScheduleService;
import com.hiyiin.IntelligentHospital.model.hosp.Department;
import com.hiyiin.IntelligentHospital.model.hosp.Hospital;
import com.hiyiin.IntelligentHospital.model.hosp.Schedule;
import com.hiyiin.IntelligentHospital.vo.hosp.DepartmentQueryVo;
import com.hiyiin.IntelligentHospital.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    HospitalService hospitalService;
    @Autowired
    HospitalSetService hospitalSetService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    ScheduleService scheduleService;

    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        Map<String,String[]> requstMap = request.getParameterMap();
        Map<String,Object> paramMap = HttpRequestHelper.switchMap(requstMap);
        //获取医院系统中传递过来的MD5加密签名
        String hospSign = (String)paramMap.get("sign");
        String hoscode = (String)paramMap.get("hoscode");
        //根据医院传来的hospcode查询数据库中的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!hospSign.equals(signKeyMD5)){
            throw new IntelligentHospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //传输过程中"+"转换成了" ",因此需要转回来
        String logoData = (String)paramMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);

        //调用service方法插入或更新数据
        hospitalService.save(paramMap);
        return Result.ok();
    }

    //查询医院
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String,String[]> requestMap = request.getParameterMap();
        Map< String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院编号
        String hoscode = (String)paramMap.get("hoscode");

        //获取医院系统中传递过来的MD5加密签名
        String hospSign = (String)paramMap.get("sign");
        //根据医院传来的hospcode查询数据库中的签名,
        String signKey = hospitalSetService.getSignKey(hoscode);
        //进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!hospSign.equals(signKeyMD5)){
            throw new IntelligentHospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    //上传科室接口
    @PostMapping("saveDepartment")
    public Result saveDept(HttpServletRequest request){
        //获取科室传来的信息
        Map<String,String[]> requstMap = request.getParameterMap();
        Map<String,Object> paramMap = HttpRequestHelper.switchMap(requstMap);
        //获取医院系统中传递过来的MD5加密签名
        String depSign = (String)paramMap.get("sign");
        String hoscode = (String)paramMap.get("hoscode");
        //根据科室传来的hospcode查询数据库中的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!depSign.equals(signKeyMD5)){
            throw new IntelligentHospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法插入或更新数据
        departmentService.save(paramMap);
        return Result.ok();
    }

    //查询科室接口
    @PostMapping("department/list")
    public Result findDept(HttpServletRequest request){
        //获取科室传来的信息
        Map<String,String[]> requstMap = request.getParameterMap();
        Map<String,Object> paramMap = HttpRequestHelper.switchMap(requstMap);
        //获取医院系统中传递过来的MD5加密签名
        String depSign = (String)paramMap.get("sign");
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");
        Integer currentPage = StringUtils.isEmpty((String)paramMap.get("page"))
                ? 1 : Integer.parseInt((String) paramMap.get("page"));
        Integer pageSize = StringUtils.isEmpty((String)paramMap.get("limit"))
                ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        //根据科室传来的hospcode查询数据库中的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!depSign.equals(signKeyMD5)){
            throw new IntelligentHospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);
        //调用service方法
        Page<Department> page = departmentService.findPageDepartment(currentPage,pageSize,departmentQueryVo);
        return Result.ok(page);
    }

    //删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取科室传来的信息
        Map<String,String[]> requstMap = request.getParameterMap();
        Map<String,Object> paramMap = HttpRequestHelper.switchMap(requstMap);
        //获取医院系统中传递过来的MD5加密签名
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");
        //签名校验
        String depSign = (String)paramMap.get("sign");
        //根据科室传来的hospcode查询数据库中的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!depSign.equals(signKeyMD5)){
            throw new IntelligentHospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    //上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String,String[]> requstMap = request.getParameterMap();
        Map<String,Object> paramMap = HttpRequestHelper.switchMap(requstMap);
        //签名校验
        String hoscode = (String)paramMap.get("hoscode");
        String depSign = (String)paramMap.get("sign");
        //根据科室传来的hospcode查询数据库中的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!depSign.equals(signKeyMD5)){
            throw new IntelligentHospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.save(paramMap);
        return Result.ok();
    }

    //查询排班接口
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        //获取科室传来的信息
        Map<String,String[]> requstMap = request.getParameterMap();
        Map<String,Object> paramMap = HttpRequestHelper.switchMap(requstMap);
        //获取医院系统中传递过来的MD5加密签名
        String schSign = (String)paramMap.get("sign");
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");
        Integer currentPage = StringUtils.isEmpty((String)paramMap.get("page"))
                ? 1 : Integer.parseInt((String) paramMap.get("page"));
        Integer pageSize = StringUtils.isEmpty((String)paramMap.get("limit"))
                ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        //根据科室传来的hospcode查询数据库中的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!schSign.equals(signKeyMD5)){
            throw new IntelligentHospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        //调用service方法
        Page<Schedule> page = scheduleService.findPageSchedule(currentPage,pageSize,scheduleQueryVo);
        return Result.ok(page);
    }

    //删除排班接口
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String,String[]> requstMap = request.getParameterMap();
        Map<String,Object> paramMap = HttpRequestHelper.switchMap(requstMap);
        //获取医院系统中传递过来的MD5加密签名
        String hoscode = (String)paramMap.get("hoscode");
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        //签名校验
        String schSign = (String)paramMap.get("sign");
        //根据科室传来的hospcode查询数据库中的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!schSign.equals(signKeyMD5)){
            throw new IntelligentHospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }
}